package com.qxzn.app.personalcenter.activity;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.alibaba.fastjson.JSONObject;
import com.qxzn.app.api.util.EventBusManager;
import com.qxzn.app.bean.BaseBean;
import com.qxzn.app.bean.EventBusBean;
import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.page.interfaces.MyInterface;
import com.qxzn.app.page.model.LoginAndRegisterModel;
import com.qxzn.app.personalcenter.R;
import com.qxzn.app.personalcenter.R2;
import com.qxzn.app.util.AppUtil;
import com.qxzn.app.util.CommonUtil;
import com.qxzn.app.util.Constant;
import com.qxzn.app.util.SharePreferenceUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;

/**
 * 账号密码登录
 */
public class LoginByPwdActivity extends BaseActivity implements MyInterface.LoginListener {

    @BindView(R2.id.et_phone)
    EditText etPhone;
    @BindView(R2.id.et_pwd)
    EditText etPwd;
    private LoginAndRegisterModel loginAndRegisterModel;
    private String phone;
    private String pwd;

    @Override
    protected int initLayout() {
        return R.layout.activity_login_by_pwd;
    }

    @Override
    protected void initView() {
        etPwd.setFilters(new InputFilter[]{new InputFilter.LengthFilter(16), new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                Pattern p = Pattern.compile("([\\ \\\\\\[\\]!\"#$%&'()*+,-./:;<=>?@^_`{|}~]|[0-9a-zA-Z])+");
                Matcher m = p.matcher(source.toString());
                if (!m.matches()) return "";
                return null;
            }
        }});
    }

    @Override
    protected void iniTitleBar() {
        tv_title.setVisibility(View.GONE);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        loginAndRegisterModel = new LoginAndRegisterModel(this);
        loginAndRegisterModel.setLoginListener(this);
        if (!TextUtils.isEmpty(SharePreferenceUtil.getUserName(this))) {
            etPhone.setText(SharePreferenceUtil.getUserName(this));
        }
        if (!TextUtils.isEmpty(SharePreferenceUtil.getPwd(this))) {
            etPwd.setText(SharePreferenceUtil.getPwd(this));
        }
    }

    @OnClick({R2.id.tv_get_code, R2.id.tv_register, R2.id.tv_forget_pwd})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.tv_get_code) {
            phone = etPhone.getText().toString().trim();
            if (TextUtils.isEmpty(phone)) {
                AppUtil.showShortToast(this, etPhone.getHint().toString());
                return;
            }
            if (!CommonUtil.isMobile(phone)) {
                AppUtil.showShortToast(this, "手机号码不正确，请重新输入");
                return;
            }
            pwd = etPwd.getText().toString().trim();
            if (TextUtils.isEmpty(pwd)) {
                AppUtil.showShortToast(this, etPwd.getHint().toString());
                return;
            }
            JSONObject params = new JSONObject();
            params.put("username", phone);
            params.put("userpwd", pwd);
            showProgress();
            loginAndRegisterModel.login(params);
        } else if (i == R.id.tv_register) {
            startActivity(new Intent(this, RegisterPhoneActivity.class));
        } else if (i == R.id.tv_forget_pwd) {
            startActivity(new Intent(this, ForgetPwdActivity.class));
        }
    }

    @Override
    public void onLoginSuccess(BaseBean baseBean) {
        hideProgress();
        if (baseBean != null) {
            if (baseBean.getCode() == 1) {
                startActivity(new Intent().setComponent(new ComponentName(this, "com.qxzn.app.desktoprobot.MainActivity")));
                if (!TextUtils.equals(SharePreferenceUtil.getUserName(this),phone)){
                    SharePreferenceUtil.setUserName(this, phone);
                    SharePreferenceUtil.setDeviceId(this, "");
                    SharePreferenceUtil.setDeviceName(this, "");
                    SharePreferenceUtil.setNickname(this, "");
                    SharePreferenceUtil.setPhone(this, "");
                    SharePreferenceUtil.setPortrait(this,"");
                }
                SharePreferenceUtil.setPwd(this, pwd);
                JPushInterface.setAlias(LoginByPwdActivity.this, 1, phone);
                EventBusManager.post(new EventBusBean.Builder().id(Constant.EventBusId.LOGIN_SUCCESS).build());
                finish();
                return;
            }
        }
        if (baseBean != null && !TextUtils.isEmpty(baseBean.getMsg())) {
            AppUtil.showToast(this, baseBean.getMsg());
        } else {
            AppUtil.showToast(this, "登录失败，请重试");
        }
    }

    @Override
    public void onLoginFailure() {
        hideProgress();
        AppUtil.showToast(this, "网络错误，请稍后再试！");
    }

    @Override
    protected void onDestroy() {
        if (loginAndRegisterModel != null) {
            loginAndRegisterModel.removeListener();
        }
        super.onDestroy();
    }
}
